ManualWindow = {}

ManualWindow.data = {}            -- data we receive from C
ManualWindow.orderTable = {}
ManualWindow.listBoxData = {}     -- same data, but organized in such a way that ListBox can display it.
ManualWindow.rowToEntryMap = {}   -- map used to go from the # of the row to the entry in ManualWindow.data
ManualWindow.numTotalRows = 0                -- this value is used to count how many rows are currently visible in the listbox
ManualWindow.lastPressedButtonId = 0

function ManualWindow.Initialize()
    LabelSetText( "ManualWindowTitleBarText", GetHelpString(StringTables.Help.TEXT_HELP_TITLE_BAR) )
    LabelSetText( "ManualWindowHeaderText", GetHelpString( StringTables.Help.BUTTON_HELP_MANUAL) )

	ButtonSetText("ManualWindowBackButton", GetHelpString( StringTables.Help.BUTTON_BACK))
    
    ManualWindow.data = EA_Window_HelpGetTopicList()
    ManualWindow.PrepareData()
    ManualWindow.UpdateManualWindowRow()
    
    -- setting up a default message
    LabelSetText( "ManualWindowTopicLabel", GetHelpString(StringTables.Help.LABEL_CHOOSE_A_TOPIC) )
    LabelSetText( "ManualWindowMainText", L"" )
    ManualWindow.SetListRowTints()
    -- open the first entry in the first category
    -- I'd rather have a default message in the Manual window text box but oh wel..
    if( ManualWindow.data )
    then
        ManualWindow.DisplayRow( 1 )
        ManualWindow.DisplayRow( 2 )
    end
end

function ManualWindow.OnShown()
end

function ManualWindow.ToggleShowing()
end

function ManualWindow.Show()
    WindowSetShowing("ManualWindow", true)
end

function ManualWindow.Hide()
    WindowSetShowing("ManualWindow", false)
end

function ManualWindow.Shutdown()
end

function ManualWindow.UpdateManualWindowRow()
    -- currently unused
end

function ManualWindow.OnRButtonUpPlayerRow()
    -- currently unused
end

-- function not yet applied anywhere.
-- Figure out why can't do SetTintColor on just a label instead of the entire folder..
function ManualWindow.MouseOverRow()
    local row = WindowGetId( SystemData.ActiveWindow.name )
    local targetRowWindow = "ManualWindowPlayerListRow"..row
    DefaultColor.SetLabelColor(targetRowWindow.."Name", DefaultColor.MAGENTA)
end

-- Processes the information stored in ManualWindow.data in order to display it in the listbox.
function ManualWindow.PrepareData()

    -- good code for seeing what's inside of data and if sorting happened at all
    
----    for sectionIndex, sectionData in ipairs( ManualWindow.data ) do
        ----DEBUG(L"ManualWindow.data["..sectionIndex..L"].id: "..sectionData.id)
----        for entryIndex, entryData in ipairs( sectionData.entries ) do
----            DEBUG(L"ManualWindow.data["..sectionIndex..L"].entries["..entryIndex..L"].id: "..entryData.id)
----            DEBUG(L"ManualWindow.data["..sectionIndex..L"].entries["..entryIndex..L"].name: "..entryData.name)
----        end
----    end
            
    -- reset tables and counters that are going to be used later on
    ManualWindow.orderTable = {}
    ManualWindow.listBoxData = {}
    ManualWindow.numTotalRows = 1                -- more of an index than row counter
    ManualWindow.rowToEntryMap = {}
    
    if( not ManualWindow.data ) then return end   -- quit if we have no data to work on.
    
    --table.sort( ManualWindow.data, DataUtils.AlphabetizeByNames ) -- sort the sections first
    for sectionIndex, sectionData in ipairs( ManualWindow.data ) do   -- iterate once per section
        --[[if( sectionData.expanded == false ) then    -- hide the minuses if collapsed, hide the pluses if expanded
            WindowSetShowing( "ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."MinusButton", false )
            WindowSetShowing( "ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."PlusButton", true )
        else
            WindowSetShowing( "ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."MinusButton", true )
            WindowSetShowing( "ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."PlusButton", false )
        end--]]
        
        -- The row contains a section, paint it gold
        --ButtonSetTextColor("ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."Name",Button.ButtonState.NORMAL, DefaultColor.GOLD.r, DefaultColor.GOLD.g, DefaultColor.GOLD.b)
        
        --table.sort( sectionData.entries, DataUtils.AlphabetizeByNames ) -- sort individual entries in the table (What if no entries? Hopefully doesn't crash, but then what's the point of a section without entries?!)
        local sectionTable = {name = sectionData.name, isSection = true, id = sectionData.id}
        table.insert( ManualWindow.listBoxData, ManualWindow.numTotalRows, sectionTable ) -- add the section table to ManualWindow.listBoxData[ManualWindow.numTotalRows]
        table.insert( ManualWindow.orderTable, ManualWindow.numTotalRows ) -- save the row to the order table, so we know that we want to display it later
        local indexStruct = {index = sectionIndex, isSection = true}
        table.insert( ManualWindow.rowToEntryMap, ManualWindow.numTotalRows, indexStruct ) -- save the position of an entry in ManualWindow.data with respect to its row
        ManualWindow.numTotalRows = ManualWindow.numTotalRows + 1
        if( sectionData.expanded == true ) then -- we check if the category is expanded, and if so, we loop on its entries
            for entryIndex, entryData in ipairs( sectionData.entries ) do -- loop once per each entry of the section
                --WindowSetShowing( "ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."MinusButton", false )
                --WindowSetShowing( "ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."PlusButton", false )
                --ButtonSetTextColor("ManualWindowPlayerListRow"..ManualWindow.numTotalRows.."Name",Button.ButtonState.NORMAL, DefaultColor.CLEAR_WHITE.r, DefaultColor.CLEAR_WHITE.g, DefaultColor.CLEAR_WHITE.b)
                local entryTable = {name = L"   "..entryData.name, isSection = false, id = entryData.id}
                table.insert( ManualWindow.listBoxData, ManualWindow.numTotalRows, entryTable )
                table.insert(ManualWindow.orderTable, ManualWindow.numTotalRows)
                local indexStruct = {categoryIndex = sectionIndex, index = entryIndex, isSection = false}
                table.insert( ManualWindow.rowToEntryMap, ManualWindow.numTotalRows, indexStruct )
                ManualWindow.numTotalRows = ManualWindow.numTotalRows + 1
            end
        end
    end
    
    ListBoxSetDisplayOrder("ManualWindowPlayerList", ManualWindow.orderTable )
    ManualWindow.SetListRowTints()
    ManualWindow.UpdateColorsAndSigns()
    ManualWindow.ResetAllButtons()
end

-- function that colors the background of the Manual sections
function ManualWindow.SetListRowTints()
    for row = 1, ManualWindow.numTotalRows do
        local row_mod = math.mod(row, 2)
        color = DataUtils.GetAlternatingRowColor( row_mod )
        
        local targetRowWindow = "ManualWindowPlayerListRow"..row
        WindowSetTintColor(targetRowWindow.."RowBackground", color.r, color.g, color.b )
        WindowSetAlpha(targetRowWindow.."RowBackground", color.a )
    end
end

-- function that gets called on left click on an entry
function ManualWindow.OnLButtonUpPlayerRow()
    local row = WindowGetId( SystemData.ActiveWindow.name )
    ManualWindow.DisplayRow(ListBoxGetDataIndex("ManualWindowPlayerList", row))
    ManualWindow.UpdateColorsAndSigns()
end

-- resets last entry selection
function ManualWindow.ResetPressedButton()
    ManualWindow.SetEntrySelectedById( ManualWindow.lastPressedButtonId, false )
end

function ManualWindow.ResetAllButtons()
    for index, data in ipairs( ManualWindow.listBoxData ) do
        --DEBUG(L"Index "..index)
        if( ManualWindow.lastPressedButtonId == data.id and data.isSection == false ) then
            ButtonSetPressedFlag("ManualWindowPlayerListRow"..index.."Name", true ) -- set newly selected entry as pressed
            --DEBUG(L"Set pressed")
        else
            ButtonSetPressedFlag("ManualWindowPlayerListRow"..index.."Name", false )
            --DEBUG(L"Set unpressed")
        end
    end
end

-- displays the entry information for a certain row
function ManualWindow.DisplayRow( row )
    local index = ManualWindow.rowToEntryMap[row].index   -- get the index in ManualWindow.data from the row
    if( ManualWindow.rowToEntryMap[row].isSection ) then
        if( ManualWindow.data[index].expanded ) then  -- expand if not expanded, otherwise collapse
            ManualWindow.data[index].expanded = false
        else
            ManualWindow.data[index].expanded = true
        end
    else
        ManualWindow.ResetPressedButton()
        local categoryIndex = ManualWindow.rowToEntryMap[row].categoryIndex
        ManualWindow.lastPressedButtonId = ManualWindow.data[categoryIndex].entries[index].id
        ButtonSetPressedFlag("ManualWindowPlayerListRow"..row.."Name", true ) -- set newly selected entry as pressed
        ManualWindow.DisplayManualEntry( ManualWindow.data[categoryIndex].entries[index].id, ManualWindow.data[categoryIndex].entries[index].name ) -- display text for the just selected entry
    end
        
    ManualWindow.PrepareData()
end

function ManualWindow.SetEntrySelectedById( id, selected )
    for index, data in pairs( ManualWindow.listBoxData ) do
        if( data.isSection == false and data.id == ManualWindow.lastPressedButtonId ) then
            ButtonSetPressedFlag("ManualWindowPlayerListRow"..index.."Name", selected ) -- set newly selected entry as pressed
        end
    end
end

function ManualWindow.DisplayManualEntry( id, name )
    local entryData = EA_Window_HelpGetEntryData( id )
    LabelSetText( "ManualWindowTopicLabel", name )        -- display the title for the entry
    LabelSetText( "ManualWindowMainText", entryData.text )-- display the entry's text
    ScrollWindowSetOffset( "ManualWindowMain", 0 )        -- reset the scroll bar.
    ScrollWindowUpdateScrollRect("ManualWindowMain")      -- reset the scroll bar.  
end

function ManualWindow.OnLButtonUpBackButton()
	ManualWindow.Hide()
	EA_Window_Help.OnShown()
end

function ManualWindow.UpdateColorsAndSigns()
    for index = 1, ManualWindowPlayerList.numVisibleRows do
        WindowSetShowing( "ManualWindowPlayerListRow"..index.."MinusButton", false )
        WindowSetShowing( "ManualWindowPlayerListRow"..index.."PlusButton", false )        
    end                     
end
